function [Tabout] = Runregsf(pric,yiel,dpric,dyiel,sambeg,samend)
% Runs regression for 
% (1) monthly&annual changes 
% (2) 33 and 50% cutoffs
% regressions: regstats2 and regstats2NW12


% *** Monthly Changes - align sample lengths
pric  = pric(dpric>=sambeg &dpric<=samend );
yiel  = yiel(dyiel>=sambeg &dyiel<=samend );

Dpric = log(pric(2:end)./ pric(1:end-1));
Dyiel = log((1+yiel(2:end)/100)./(1+yiel(1:end-1)/100));

% **** 33 cutoff Create low and high rate samples
cuto = 33;
sampfilL = yiel(2:end)<=prctile(yiel(2:end),cuto);    
sampfilH = yiel(2:end)>prctile(yiel(2:end),cuto); 

%Run regressions
stats = regstats2( Dpric(sampfilL), Dyiel(sampfilL),'linear');  beta1=stats.beta; pval1=stats.hac.pval; rsq1=stats.adjrsquare; 
stats = regstats2( Dpric(sampfilH), Dyiel(sampfilH),'linear');  beta2=stats.beta; pval2=stats.hac.pval; rsq2=stats.adjrsquare; 
stats = regstats2( Dpric, Dyiel,'linear');                      beta3=stats.beta; pval3=stats.hac.pval; rsq3=stats.adjrsquare; 

YL  = [beta1(2); rsq1];
YLp{1} = staf(pval1(2));
YLp{2} = ' ';
YH  = [beta2(2); rsq2];
YHp{1} = staf(pval2(2));
YHp{2} = ' ';
Y   = [beta3(2); rsq3];
Yp{1} = staf(pval3(2));
Yp{2} = ' ';


% **** 50 cutoff Create low and high rate samples
cuto = 50;
sampfilL = yiel(2:end)<=prctile(yiel(2:end),cuto);    
sampfilH = yiel(2:end)>prctile(yiel(2:end),cuto); 

%Run regressions
stats = regstats2( Dpric(sampfilL), Dyiel(sampfilL),'linear');  beta1=stats.beta; pval1=stats.hac.pval; rsq1=stats.adjrsquare; 
stats = regstats2( Dpric(sampfilH), Dyiel(sampfilH),'linear');  beta2=stats.beta; pval2=stats.hac.pval; rsq2=stats.adjrsquare; 
stats = regstats2( Dpric, Dyiel,'linear');                      beta3=stats.beta; pval3=stats.hac.pval; rsq3=stats.adjrsquare; 

YL  = [YL;  beta1(2); rsq1];
YLp{3} = staf(pval1(2));
YLp{4} = ' ';
YH  = [YH;  beta2(2); rsq2];
YHp{3} = staf(pval2(2));
YHp{4} = ' ';
Y   = [Y;   beta3(2); rsq3];
Yp{3} = staf(pval3(2));
Yp{4} = ' ';


%%%%%%%%%%%% robustness checks
uyes=0;
if uyes==1;
% Regression with interactive terms
stats = regstats2( Dpric, [Dyiel yiel(2:end) yiel(2:end).*Dyiel]  ,'linear');
beta4=stats.beta; pval4=stats.hac.pval; rsq4=stats.adjrsquare; 

[beta4 pval4]
rsq4
pause
end
%%%%%%%%%%%%%%%%%%


% *** Annual Changes
Dpric = log(   pric(1+12:end)./        pric(1:end-12));
Dyiel = log((1+yiel(1+12:end)/100)./(1+yiel(1:end-12)/100));

% **** 33 cutoff Create low and high rate samples
cuto = 33;
sampfilL = yiel(1+12:end)<=prctile(yiel(1+12:end),cuto);    
sampfilH = yiel(1+12:end)> prctile(yiel(1+12:end),cuto); 

%Run regressions
stats = regstats2NW12( Dpric(sampfilL), Dyiel(sampfilL),'linear');  beta1=stats.beta; pval1=stats.hac.pval; rsq1=stats.adjrsquare; 
stats = regstats2NW12( Dpric(sampfilH), Dyiel(sampfilH),'linear');  beta2=stats.beta; pval2=stats.hac.pval; rsq2=stats.adjrsquare; 
stats = regstats2NW12( Dpric, Dyiel,'linear');                      beta3=stats.beta; pval3=stats.hac.pval; rsq3=stats.adjrsquare; 

YL  = [YL;  beta1(2); rsq1];
YLp{5} = staf(pval1(2));
YLp{6} = ' ';
YH  = [YH;  beta2(2); rsq2];
YHp{5} = staf(pval2(2));
YHp{6} = ' ';
Y   = [Y;   beta3(2); rsq3];
Yp{5} = staf(pval3(2));
Yp{6} = ' ';

% **** 33 cutoff Create low and high rate samples
cuto = 50;
sampfilL = yiel(1+12:end)<=prctile(yiel(1+12:end),cuto);    
sampfilH = yiel(1+12:end)> prctile(yiel(1+12:end),cuto); 

%Run regressions
stats = regstats2NW12( Dpric(sampfilL), Dyiel(sampfilL),'linear');  beta1=stats.beta; pval1=stats.hac.pval; rsq1=stats.adjrsquare; 
stats = regstats2NW12( Dpric(sampfilH), Dyiel(sampfilH),'linear');  beta2=stats.beta; pval2=stats.hac.pval; rsq2=stats.adjrsquare; 
stats = regstats2NW12( Dpric, Dyiel,'linear');                      beta3=stats.beta; pval3=stats.hac.pval; rsq3=stats.adjrsquare; 

YL  = [YL;  beta1(2); rsq1];
YLp{7} = staf(pval1(2));
YLp{8} = ' ';
YH  = [YH;  beta2(2); rsq2];
YHp{7} = staf(pval2(2));
YHp{8} = ' ';
Y   = [Y;   beta3(2); rsq3];
Yp{7} = staf(pval3(2));
Yp{8} = ' ';

TStat = {'Mon, 33% cutof: slope';'Rsquare'; 'Mon, 50% cutof: slope';'Rsquare';'Ann, 33% cutof: slope';'Rsquare'; 'Ann, 50% cutof: slope';'Rsquare' };

Tabout = table(TStat,YL,YLp',YH,YHp',Y,Yp');
end

